package com.im.users.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.im.security.dto.LoginVo;
import com.im.users.entity.FansEntity;
import com.im.users.entity.FollowerEntity;
import com.im.users.entity.UserEntity;
import com.im.users.mapper.FansDao;
import com.im.users.mapper.UserDao;
import com.im.users.service.FansService;
import com.im.users.service.FollowerService;
import com.im.users.service.UserService;
import com.im.users.vo.FansVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.im.common.utils.PageUtils;
import com.im.common.utils.Query;

@Slf4j
@Service("fansService")
public class FansServiceImpl extends ServiceImpl<FansDao, FansEntity> implements FansService {

    @Autowired
    private UserService userService;

    @Autowired
    private FollowerService followerService;

    @Autowired
    private UserDao userDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<FansEntity> page = this.page(
                new Query<FansEntity>().getPage(params),
                new QueryWrapper<FansEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public List<FansVo> getFansList(LoginVo loginVo) {
        LambdaQueryWrapper<FansEntity> queryWrapper = new LambdaQueryWrapper<FansEntity>()
                .eq(FansEntity::getUserNum, loginVo.getIdNum());
        List<FansEntity> fansEntityList = this.list(queryWrapper);
        ArrayList<FansVo> fansVos = new ArrayList<>();
        log.debug("fansEntityList:{}",fansEntityList);
        ArrayList<Integer> list = new ArrayList<>();
        if (!CollectionUtil.isEmpty(fansEntityList)) {
            for (FansEntity fansEntity : fansEntityList) {
                list.add(fansEntity.getFansNum());
            }
            LambdaQueryWrapper<UserEntity> wrapper = new LambdaQueryWrapper<UserEntity>()
                    .in(UserEntity::getIdNum, list);
            List<UserEntity> entityList = userService.list(wrapper);
//        List<UserEntity> entityList = userDao.getUsersByNums(fansEntityList);

            for (UserEntity userEntity : entityList) {
                FansVo fansVo = new FansVo();
                BeanUtils.copyProperties(userEntity, fansVo);
//            LambdaQueryWrapper<FollowerEntity> eq = new LambdaQueryWrapper<FollowerEntity>()
//                    .eq(FollowerEntity::getUserNum, userEntity.getIdNum())
//                    .eq(FollowerEntity::getFollowerNum, loginVo.getIdNum());
//            FollowerEntity one = followerService.getOne(eq);
//            if (ObjectUtil.isEmpty(one)){
//                fansVo.setFollow(2);
//            }else {
//            }
                fansVo.setFollow(1);
                fansVos.add(fansVo);
            }
        }
        return fansVos;
    }

    public Long getFansCount(Integer idNum) {
        LambdaQueryWrapper<FansEntity> queryWrapper = new LambdaQueryWrapper<FansEntity>()
                .eq(FansEntity::getUserNum, idNum);
        long fansCount = this.count(queryWrapper);
        return fansCount;
    }
}